package com.wbb.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.wbb.bean.LoginUsernamePasswordToken;
import com.wbb.constant.LoginConstants;
import com.wbb.constant.ResultConstants;
import com.wbb.model.TbAdmin;
import com.wbb.model.TbSeller;
import com.wbb.result.JsonResult;
import com.wbb.util.JwtUtil;
import com.wbb.vo.AdminVo;
import com.wbb.vo.SellerVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/adminLogin")
    public JsonResult adminLogin(@RequestBody Map<String, String> map){
        String username = map.get("username");
        String password = map.get("password");
        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)){
            return JsonResult.response(ResultConstants.PARAMS_NULL); // code: 3
        }

        // 传参
        LoginUsernamePasswordToken usernamePasswordToken = new LoginUsernamePasswordToken(username, password, LoginConstants.ADMIN);

        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e){
            return JsonResult.response(ResultConstants.ACCOUNT_INEXISTENCE); // code: 4
        }catch (IncorrectCredentialsException e){
            return JsonResult.response(ResultConstants.PASSWORD_ERROR); // code: 5
        }
        TbAdmin admin = (TbAdmin) subject.getPrincipal();
        AdminVo adminVo = new AdminVo();
        BeanUtils.copyProperties(admin,adminVo);
        String strAdmin = JSONObject.toJSONString(adminVo);
        String token = jwtUtil.createJwt(admin.getId().toString(), strAdmin, "roles");
        return JsonResult.response(ResultConstants.SUCCESS_LOGIN).setData(token); // code: 2
    }

    @PostMapping("/sellerLogin")
    public JsonResult sellerLogin(@RequestBody Map<String, String> map){
        String username = map.get("username");
        String password = map.get("password");
        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)){
            return JsonResult.response(ResultConstants.PARAMS_NULL); // code: 3
        }

        // 传参
        LoginUsernamePasswordToken usernamePasswordToken = new LoginUsernamePasswordToken(username, password, LoginConstants.SELLER);

        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e){
            return JsonResult.response(ResultConstants.ACCOUNT_INEXISTENCE);
        }catch (IncorrectCredentialsException e){
            return JsonResult.response(ResultConstants.PASSWORD_ERROR);
        }
        TbSeller seller = (TbSeller) subject.getPrincipal();
        SellerVo sellerVo = new SellerVo();
        BeanUtils.copyProperties(seller,sellerVo);
        String strSeller = JSONObject.toJSONString(sellerVo);
        String token = jwtUtil.createJwt(seller.getSellerId(), strSeller, "roles");
        return JsonResult.response(ResultConstants.SUCCESS_LOGIN).setData(token);
    }
}
